<?php
	class Manage_warranty extends CI_Controller{
		public function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->library('form_validation');
			$this->load->library('cismarty');
			$this->load->model("apanel/template");
			$this->load->model("apanel/db_user");
			$this->load->model("apanel/db_news");
			$this->load->model("apanel/db_info");
			$this->load->model("apanel/db_warranty");
			$this->load->library('pagination');
		}
		
		public function index(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			redirect("manage_warranty/war_list");
		}
		
		public function war_list($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			//list($item,$num,$page,$total) = $this->listall();
			//$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			$scripts = '
			<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
			$(\'#table1\').dataTable( {
			"bJQueryUI": true,
			"bProcessing": true,
			"bServerSide": true,
			"sAjaxSource": "'.base_url().'manage_warranty/datalist",
			"iDisplayStart"  : 0,
			"sPaginationType": "full_numbers",
			"aaSorting": [[ 0, "desc" ]],
			"aoColumnDefs": [
			{ "bSortable": false, "aTargets": [ 0 ] },
			{ "bSortable": false, "aTargets": [ 4 ] },
			{ "bSortable": false, "aTargets": [ 5 ] },
			],
				"fnServerData": function(sSource, aoData, fnCallback)
			{
			$.ajax(
			{
			\'dataType\': \'json\',
			\'type\'  : \'POST\',
			\'url\'    : sSource,
			\'data\'  : aoData,
			\'success\' : fnCallback
			}
			);
			}
			} );
			} );
			</script>';
		
			//== template
			$this->cismarty->assign("scripts",$scripts);
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,6));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open('manage_warranty/war_list'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/warranty/war_list.html');
		}
		
		public function datalist(){
			$output = $this->db_warranty->datalist();
			echo json_encode($output);
		}
		
	
		
		public function war_detail($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			$country = "";
			$level  ="";
			$group = "";
			if($id != ""){
				$this->db->where("pkid",$id);
				$res = $this->db->get('mobile_warranty');
				$row = $res->row();
			}
			if($this->input->post('bntSubmit') != null){
				
				
					$data = array(
					
							"status"=>$this->input->post("status"),
						
					);
					$this->db->where("pkid",$id);
					$res = $this->db->update('mobile_warranty',$data);
					if($res) redirect('manage_warranty/war_list/success');
					else  redirect('manage_warranty/war_list/fail');
	
					
			}else{
				$this->db->where("pkid",$id);
				$res = $this->db->get('mobile_warranty');
				$row = $res->row();
				$this->cismarty->assign("model_no",$row->model_no);
				$this->cismarty->assign("serial_no",$row->serial_no);
				$this->cismarty->assign("purchase_date",$row->purchase_date);
				$this->cismarty->assign("warranty_period",$row->warranty_period);
				if($row->status == 1)
					 $this->cismarty->assign("sa",'checked="checked"');
				else 
					$this->cismarty->assign("sd",'checked="checked"');
					
				if($row->warranty_unit == 'Y')
					 $this->cismarty->assign("unit","Year");
				else 
					$this->cismarty->assign("unit","Month");
					
				$this->db->where("pkid",$row->member_pkid);
				$res1 = $this->db->get('mobile_member');
				$row1 = $res1->row();
				if($row1!=""){
				$this->cismarty->assign("name",$row1->name);
				}else{
				$this->cismarty->assign("name","");
				}
				
				$this->db->where("pkid",$row->product_pkid);
				$res2 = $this->db->get('mobile_product_item');
				$row2 = $res2->row();
				$this->cismarty->assign("name_en",$row2->name_en);
				
				
				
			}
			
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,6));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_warranty/war_detail/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/warranty/war_detail.html');
		}
		
		
		
	}

?>